# -*- coding: utf-8 -*-
"""
Created on Tue May 23 14:55:54 2023

@author: zh-gsyw-wn
"""
import pandas as pd
from scipy.stats import norm
import matplotlib.pyplot as plt

def get_index_score(data,df_dim,model_type,batch_id):
    data['index_code2'] = data.var_name.map(df_dim.set_index('var_name').lvl_2)
    data['index_code3'] = data.var_name.map(df_dim.set_index('var_name').lvl_3)
    
    index_score2 = data.groupby(['rating_id','index_code2']).weight_score.sum().reset_index()
    index_score2['batch_id'] = batch_id
    index_score2['model_type'] = model_type
    index_score3 = data.groupby(['rating_id','index_code3']).weight_score.sum().reset_index()
    index_score3['batch_id'] = batch_id
    index_score3['model_type'] = model_type
    index_score2.rename(columns={'index_code2':'index_code'},inplace=True)
    index_score3.rename(columns={'index_code3':'index_code'},inplace=True)
    return index_score2,index_score3




# def get_level(result):    
#     '''
#     输入esg分数表
#     返回增加了esg等级的esg分数表
#     '''
#     print('')
#     print('开始添加esg等级')
#     result['rank_'] = result.groupby('model_type').esg_score.rank(ascending=False,pct=True)
    
#     result['esg_rating'] = 1
#     result.loc[result.rank_ <= 0.8,'esg_rating'] = 2
#     result.loc[result.rank_ <= 0.6,'esg_rating'] = 3
#     result.loc[result.rank_ <= 0.4,'esg_rating'] = 4
#     result.loc[result.rank_ <= 0.2,'esg_rating'] = 5
#     result.loc[result.rank_.isna(),'esg_rating'] = 0
    
#     result.esg_rating = result.esg_rating.map({5:'A',4:'B',3:'C',2:'D',1:'E',0:'M'})
#     del result['rank_']
#     result = result.reset_index(drop = True)
#     print('等级添加完成')
#     print('')
#     return result 


def get_level(result,model_type):
    print('开始计算等级')
    result['esg_rating'] = 'M'
    mean = result.esg_score.mean()
    std = result.esg_score.std()
    bin_edges = [norm.ppf(i/5, mean, std) for i in range(6)]
    print('{}分箱边缘：'.format(model_type),bin_edges)
    result['esg_rating'] = pd.cut(result['esg_score'], bin_edges, labels=['E','D','C','B','A'])
    result.esg_rating = result.esg_rating.astype('str')
    plt.figure(figsize = (8,6))
    # result['esg_rating'].value_counts().sort_index().plot(kind='bar')    

    result = result.reset_index(drop = True)
    print('等级已经计算完成')
    return result


def get_level2(result,model_type):
    print('开始计算等级')
    result['esg_rating2'] = 'M'
    df_E = result.loc[result.esg_score < 60]
    df_E.esg_rating2 = 'E'
    result = result.loc[result.esg_score >= 60]
    mean = result.esg_score.mean()
    std = result.esg_score.std()
    bin_edges = [norm.ppf(i/4, mean, std) for i in range(5)]
    print('{}分箱边缘：'.format(model_type),bin_edges)
    result['esg_rating2'] = pd.cut(result['esg_score'], bin_edges, labels=['D','C','B','A'])
    
    result = pd.concat([df_E,result])
    
    result.esg_rating2 = result.esg_rating2.astype('str')
    plt.figure(figsize = (8,6))
    # result['esg_rating2'].value_counts().sort_index().plot(kind='bar')    

    result = result.reset_index(drop = True)
    print('等级已经计算完成')
    return result
